package com.ruoyi.crm.sale.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.sale.domain.CrmBusinessStatus;
import com.ruoyi.crm.sale.mapper.CrmBusinessTypeMapper;
import com.ruoyi.crm.sale.domain.CrmBusinessType;
import com.ruoyi.crm.sale.service.ICrmBusinessTypeService;

/**
 * 商机状态组类别Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class CrmBusinessTypeServiceImpl implements ICrmBusinessTypeService 
{
    @Autowired
    private CrmBusinessTypeMapper crmBusinessTypeMapper;

    /**
     * 查询商机状态组类别
     * 
     * @param id 商机状态组类别ID
     * @return 商机状态组类别
     */
    @Override
    public CrmBusinessType selectCrmBusinessTypeById(Long id)
    {
        return crmBusinessTypeMapper.selectCrmBusinessTypeById(id);
    }

    /**
     * 查询商机状态组类别列表
     * 
     * @param crmBusinessType 商机状态组类别
     * @return 商机状态组类别
     */
    @Override
    public List<CrmBusinessType> selectCrmBusinessTypeList(CrmBusinessType crmBusinessType)
    {
        return crmBusinessTypeMapper.selectCrmBusinessTypeList(crmBusinessType);
    }

    /**
     * 新增商机状态组类别
     * 
     * @param crmBusinessType 商机状态组类别
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCrmBusinessType(CrmBusinessType crmBusinessType)
    {
    	crmBusinessType.setCreateTime(DateUtils.getNowDate());
    	crmBusinessType.setUpdateTime(DateUtils.getNowDate());
    	crmBusinessType.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int rows = crmBusinessTypeMapper.insertCrmBusinessType(crmBusinessType);
        insertCrmBusinessStatus(crmBusinessType);
        return rows;
    }

    /**
     * 修改商机状态组类别
     * 
     * @param crmBusinessType 商机状态组类别
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCrmBusinessType(CrmBusinessType crmBusinessType)
    {
    	crmBusinessType.setUpdateTime(DateUtils.getNowDate());
    	crmBusinessType.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        crmBusinessTypeMapper.deleteCrmBusinessStatusByTypeId(crmBusinessType.getId());
        insertCrmBusinessStatus(crmBusinessType);
        return crmBusinessTypeMapper.updateCrmBusinessType(crmBusinessType);
    }

    /**
     * 批量删除商机状态组类别
     * 
     * @param ids 需要删除的商机状态组类别ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCrmBusinessTypeByIds(Long[] ids)
    {
        crmBusinessTypeMapper.deleteCrmBusinessStatusByTypeIds(ids);
        return crmBusinessTypeMapper.deleteCrmBusinessTypeByIds(ids);
    }

    /**
     * 删除商机状态组类别信息
     * 
     * @param id 商机状态组类别ID
     * @return 结果
     */
    @Override
    public int deleteCrmBusinessTypeById(Long id)
    {
        crmBusinessTypeMapper.deleteCrmBusinessStatusByTypeId(id);
        return crmBusinessTypeMapper.deleteCrmBusinessTypeById(id);
    }

    /**
     * 新增商机状态信息
     * 
     * @param crmBusinessType 商机状态组类别对象
     */
    public void insertCrmBusinessStatus(CrmBusinessType crmBusinessType)
    {
        List<CrmBusinessStatus> crmBusinessStatusList = crmBusinessType.getCrmBusinessStatusList();
        Long id = crmBusinessType.getId();
        if (StringUtils.isNotNull(crmBusinessStatusList))
        {
            List<CrmBusinessStatus> list = new ArrayList<CrmBusinessStatus>();
            for (CrmBusinessStatus crmBusinessStatus : crmBusinessStatusList)
            {
                crmBusinessStatus.setTypeId(id);
                crmBusinessStatus.setCreateTime(DateUtils.getNowDate());
                crmBusinessStatus.setUpdateTime(DateUtils.getNowDate());
                crmBusinessStatus.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
                
                list.add(crmBusinessStatus);
            }
            if (list.size() > 0)
            {
                crmBusinessTypeMapper.batchCrmBusinessStatus(list);
            }
        }
    }
}
